using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Utilities;
using FlatRedBall.Input;
using FlatRedBall.Audio;
using MP3.Screens;

namespace MP3
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 700;
            graphics.PreferredBackBufferWidth = 1024;
            Content.RootDirectory = "Content";
			BackStack<string> bs = new BackStack<string>();
			bs.Current = string.Empty;
        }

        protected override void Initialize()
        {
            Renderer.UseRenderTargets = false;
            FlatRedBallServices.InitializeFlatRedBall(this, graphics);
			GlobalContent.Initialize();
            ControllerCheck();
			Screens.ScreenManager.Start(typeof(MP3.Screens.SplashScreen).FullName);
            base.Initialize();
            
        }


        protected override void Update(GameTime gameTime)
        {
            FlatRedBallServices.Update(gameTime);
            ScreenManager.Activity();
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            FlatRedBallServices.Draw();
            base.Draw(gameTime);
        }


        // detect 2 X-box game pads.
        // if one of the game pas is missing the keys will be binded to the keyboard.
        private void ControllerCheck()
        {

            // if not controlelr 1- map keys to keyboard
            if (FlatRedBall.Input.InputManager.Xbox360GamePads[0].IsConnected == false)
            {
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].CreateDefaultButtonMap();
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].ButtonMap.RightAnalogDown = Keys.K;
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].ButtonMap.RightAnalogUp = Keys.I;
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].ButtonMap.RightAnalogLeft = Keys.J;
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].ButtonMap.RightAnalogRight = Keys.L;
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].ButtonMap.X = Keys.P;
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].ButtonMap.A = Keys.Y;
                FlatRedBall.Input.InputManager.Xbox360GamePads[0].ButtonMap.B = Keys.U;

            }

            // if not controlelr 2- map keys to keyboard
            if (FlatRedBall.Input.InputManager.Xbox360GamePads[1].IsConnected == false)
            {
                FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap = new KeyboardButtonMap();
                FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.LeftAnalogDown = Keys.S;
                FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.LeftAnalogUp = Keys.W;
                FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.LeftAnalogLeft = Keys.A;
                FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.LeftAnalogRight = Keys.D;
                FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.Back = Keys.Z;


            }
        }
    }
}
